package com.airlines.crewassignment.controller.crewlocation;



import java.util.List;

import javax.naming.NoPermissionException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.airlines.base.entity.user.User;
import com.airlines.common.Page;
import com.airlines.common.R;
import com.airlines.crewassignment.entity.crewlocation.CrewBackup;
import com.airlines.crewassignment.service.crewlocation.CrewBackupService;

/**
 * @author xxxx
 * @date 2018/06/25 09:51:34
 * @company xxxx Airlines
 * @description 备份人员
 */
@Controller
@RequestMapping("/crewBackup")
public class CrewBackupController {

    private static final String PREFIX = "/crewlocation";

    @Autowired
    CrewBackupService crewBackupService;

    /**
     * 进入页面
     * @return
     */
    @RequestMapping(value = "", method={ RequestMethod.GET})
//    @RequiresPermissions("crewlocation:crewBackup:list")
    String toCrewBackupPage(Model model,String moduleFlag) throws NoPermissionException {
        model.addAttribute("moduleFlag",moduleFlag);
        return PREFIX + "/crewBackupList";
    }

    /**
    * 多条件模糊 查询并且分页显示
    * @param crewBackup
    * @param offset
    * @param limit
    * @return
    */
    @RequestMapping(value = "/list")
    @ResponseBody
//    @RequiresPermissions("crewlocation:crewBackup:list")
    public Page list(CrewBackup crewBackup, String offset, String limit) {
        Page page = new Page();
        page.setPageNumber((Integer.valueOf(offset) / (Integer.valueOf(limit))));
        page.setPageSize(Integer.valueOf(limit));
        page = crewBackupService.list(crewBackup, page);
        return page;
    }

    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping(value = "/add", method={ RequestMethod.GET})
//    @RequiresPermissions("crewlocation:crewBackup:add")
    String add(Model model,String moduleFlag){
        model.addAttribute("moduleFlag",moduleFlag);
        return PREFIX + "/crewBackupAdd";
    }

    /**
     * 保存
     * @param crewBackup
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method={ RequestMethod.POST})
//    @RequiresPermissions("crewlocation:crewBackup:add")
    public R save(CrewBackup crewBackup, HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("currentUser");
        crewBackup.setUpdateBy(user.getWorkcode());
        crewBackup.setBakFlag("N");//默认是N，不知道有什么用，数据库不能为空
        crewBackup.setIsDelete(0);
        if(crewBackupService.insert(crewBackup) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 进入编辑页面
     * @param model
     * @param flightDate
     * @return
     */
    @RequestMapping(value = "/edit", method={ RequestMethod.GET})
//    @RequiresPermissions("crewlocation:crewBackup:edit")
    String edit(Model model,String flightDate,String pCode,String moduleFlag){
        CrewBackup crewBackup = crewBackupService.get(flightDate,pCode);
        model.addAttribute("crewBackup", crewBackup);
        model.addAttribute("moduleFlag", moduleFlag);
        return PREFIX + "/crewBackupEdit";
    }

    /**
     * 修改
     * @param crewBackup
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method={ RequestMethod.POST})
//    @RequiresPermissions("crewlocation:crewBackup:edit")
    public R update( CrewBackup crewBackup,HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("currentUser");
        crewBackup.setUpdateBy(user.getWorkcode());
        if(crewBackupService.update(crewBackup)>0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 进入查看页面
     * @param model
     * @param flightDate
     * @return
     */
    @RequestMapping(value = "/view", method={ RequestMethod.GET})
//    @RequiresPermissions("crewlocation:crewBackup:list")
    String view(Model model,String flightDate,String pCode,String moduleFlag){
        CrewBackup crewBackup = crewBackupService.get(flightDate,pCode);
        model.addAttribute("crewBackup", crewBackup);
        model.addAttribute("isReadonly", 1);
        model.addAttribute("moduleFlag", moduleFlag);
        return PREFIX + "/crewBackupEdit";
    }

    /**
     * 删除
     * @param flightDate
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method={ RequestMethod.POST})
//    @RequiresPermissions("crewlocation:crewBackup:delete")
    public R delete( String flightDate,String pCode){
        if(crewBackupService.deleteLogic(flightDate,pCode)>0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 检查备份名称是否存在
     * @param crewBackup
     * @return
     */
    @RequestMapping("check")
    @ResponseBody
    public R check(CrewBackup crewBackup){
        boolean flag = false;
        if(StringUtils.isEmpty(crewBackup.getFlightDate()) || crewBackup.getPCode() == null){
            flag = true;
        }else{

            List<CrewBackup> crewBackups = crewBackupService.list(crewBackup);
            flag = crewBackups.size() == 0;
        }
        return R.ok().put("valid",flag);
    }

}
